package com.jian.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jian.bus.pojo.Goods;
import com.jian.bus.pojo.Inport;
import com.jian.bus.pojo.Outport;
import com.jian.bus.pojo.Provider;
import com.jian.bus.service.GoodsService;
import com.jian.bus.service.InportService;
import com.jian.bus.service.OutportService;
import com.jian.bus.service.ProviderService;
import com.jian.bus.vo.InportVo;
import com.jian.bus.vo.OutportVo;
import com.jian.sys.common.Constant;
import com.jian.sys.common.DataGridView;
import com.jian.sys.common.ResultObj;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 剑
 * @since 2020-01-23
 */
@RestController
@RequestMapping("/outport")
public class OutportController {
    @Autowired
    OutportService outportService;
    @Autowired
    InportService inportService;
    @Autowired
    GoodsService goodsService;
    @Autowired
    ProviderService providerService;
    /**
     * 查询所有和模糊查询
     */
    @RequestMapping("/loadAllOutport")
    public DataGridView loadAllOutport(OutportVo outportVo){

        IPage<Outport> page = new Page<>(outportVo.getPage(),outportVo.getLimit());
        QueryWrapper<Outport> wrapper = new QueryWrapper<>();
        wrapper.eq(outportVo.getProviderid()!=null&&outportVo.getProviderid()!=0,"providerid",outportVo.getProviderid());
        wrapper.eq(outportVo.getGoodsid()!=null&&outportVo.getGoodsid()!=0,"goodsid",outportVo.getGoodsid());
        wrapper.like(StringUtils.isNotBlank(outportVo.getOperateperson()),"operateperson",outportVo.getOperateperson());
        wrapper.like(StringUtils.isNotBlank(outportVo.getRemark()),"remark",outportVo.getRemark());
        wrapper.ge(outportVo.getStartTime()!=null,"outporttime",outportVo.getStartTime());
        wrapper.le(outportVo.getEndTime()!=null,"outporttime",outportVo.getEndTime());
        wrapper.orderByDesc("outporttime");
        this.outportService.page(page,wrapper);
        List<Outport> list = page.getRecords();
        //通过商品id设置商品名称 商品规格
        for (Outport outport : list) {
            Goods goods = this.goodsService.getById(outport.getGoodsid());
            if(null!=goods){
                outport.setGoodsname(goods.getGoodsname());
                outport.setSize(goods.getSize());
            }
        }
        //通过供应商id设置供应商名称
        for (Outport outport : list) {
            Provider provider = this.providerService.getById(outport.getProviderid());
            if(null!=provider){
                outport.setProvidername(provider.getProvidername());
            }
        }
        return new DataGridView(page.getTotal(),list);
    }
    /**
     * 添加退货信息
     * @param id
     * @param remark
     * @param number
     * @return
     */
    @RequestMapping("/addOutport")
    public ResultObj addOutport(Integer id,String remark,Integer number){
        try {
            this.outportService.saveOutportInfo(id,remark,number);
            return ResultObj.OPERATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.OPERATE_ERROR;
        }
    }

}

